var columns = [
    {id:"name", field:"name", name:i18n.file.grid.name, width:150, sortable:true, cssClass:"cell-title"},
    {id:"description", field:"description", name:i18n.file.grid.description, width:300, sortable:true, cssClass:"cell-title"},
    {id:"category", field:"category", name:i18n.file.grid.category, width:150, sortable:true, cssClass:"cell-title"},
    {id:"public", field:"public", name:i18n.file.grid.public, width:100, sortable:true, cssClass:"cell-title"},
    {id:"action", field:"action", name:i18n.file.grid.action, width:100, sortable:false, cssClass:"cell-effort-driven"}
];

var columns2 = [
    {id:"name", field:"name", name:i18n.file.grid.name, width:150, sortable:true, cssClass:"cell-title"},
    {id:"description", field:"description", name:i18n.file.grid.description, width:300, sortable:true, cssClass:"cell-title"},
    {id:"category", field:"category", name:i18n.file.grid.category, width:150, sortable:true, cssClass:"cell-title"},
    {id:"user", field:"user", name:i18n.file.grid.user, width:120, sortable:true, cssClass:"cell-title"},
    {id:"action", field:"action", name:i18n.file.grid.action, width:100, sortable:false, cssClass:"cell-effort-driven"}
];

var options = {
    editable:false,
    sortable:true,
    enableAddRow:false,
    enableCellNavigation:false,
    forceFitColumns:false,

    showColumnPicker:true,
    showPager:false,
    data:null,
    onFilter:null,
    onSort:fileSort,
    onClick:null,
    onCellPaste:null,
    columns:columns
};

var grid = new DataGrid(columns, options);
var allGrid = new DataGrid(columns2, options);

$(function () {
    $("#upload").click(uploadFile);
    $("#refreshData").click(loadFiles);
    $("#refreshAllData").click(loadAllFiles);

    $(".download").live("click", downloadFile);

    $("#login-div label.panel-header").collapsiblePanel({
        minimize:function () {
            $(this).hide();
        },
        restore:function () {
            $(this).show();
        }
    });

    $("#upload-div label.panel-header").collapsiblePanel({
        minimize:function () {
            $(this).hide();
        },
        restore:function () {
            $(this).show();
        }
    });

    $("#myfiles-div label.panel-header").collapsiblePanel({
        minimize:function () {
            $(this).hide();
        },
        restore:function () {
            $(this).show();
        }
    });

    $("#allfiles-div label.panel-header").collapsiblePanel({
        minimize:function () {
            $(this).hide();
        },
        restore:function () {
            $(this).show();
        }
    });

    //Grid config
    grid.attachTo($("#myFilesGrid"));
    allGrid.attachTo($("#allFilesGrid"));

    loadFiles();
    loadAllFiles();
});

function uploadFile() {
    var file = dwr.getValue('file');
    var description = $("#description").val();
    var category = $("#category").val();
    var isPublic = $("#public").is(":checked");
    var path = $("#file").val();

    FileServiceRemote.uploadFile(description, category, path, isPublic, file,
            {callback:function (result) {
                if (result) {
                    alert("File uploaded successfully");
                } else {
                    alert("File uploading failed. Try again later");
                }
            }, timeout:600000
            })
}

function downloadFile() {
    //var elem = $("#download").parent();
    var fileId = $(this).attr("id");

    FileServiceRemote.getFile(fileId, $(this).attr("name"),
            {callback:function (data) {
                if (data == null) {
                    alert("File download failed. Try again later");
                } else {
                    dwr.engine.openInDownload(data);
                }
            }
            });
}

function loadFiles() {
    FileServiceRemote.getUserFiles({callback:function (files) {
        $.each(files, function () {
            this.public = this.public === true ? "yes" : "no";

            this.action = "<button id='" + this.id + "' type='button' class='download' name='" + this.name + "'>Download</button>";
        });

        grid.data(files);
        grid.refresh();
    }});
}

function loadAllFiles() {
    FileServiceRemote.getAllFiles({callback:function (files) {
        $.each(files, function () {
            this.action = "<button id='" + this.id + "' type='button' class='download' name='" + this.name + "'>Download</button>";
        });

        allGrid.data(files);
        allGrid.refresh();
    }});
}

function fileSort(col, asc) {
    function _fileSort(left, right) {
        var l = left[col.field];
        var r = right[col.field];

        var result = compare(l, r);

        return result !== 0 ? result : compare(left.id, right.id);
    }

    grid.dataView.sort(_fileSort, asc);
}

function compare(left, right) {
    return left == null || left === undefined || left < right ? -1 : left == right ? 0 : 1;
}